<?php
namespace api\modules\v1\controllers;

use api\common\controllers\AController;
use common\records\UserAchievement;
use common\records\Achievement;
use yii\data\ActiveDataProvider;

class UserAchievementController extends AController
{
    public $modelClass = 'common\records\UserAchievement';
    /**
     * @api {get} /v1/userachievements 用户成就列表
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup Achievement
     *
     * @apiParam {String} uid 用户id
     *
     */
    public function actionIndex()
    {
        $uid = trim($_GET['uid']);
        //$uid="b1Z0N0w0bml1Vl9nQV96MGJheGJqd3Q5UGdZOA==";
        $user_achievement = UserAchievement::find()->where(['uid' => $uid])->asArray()->all();
        $user_achieved = array();
        foreach($user_achievement AS $item)
        {
            $user_achieved[intval($item['aid'])] = $item['timestamp'];
        }
        $achievement = Achievement::find()->where([])->asArray()->all();
        for($i = 0; $i<count($achievement); $i++)
        {
            if(array_key_exists(intval($achievement[$i]['id']), $user_achieved))
            {

                $achievement[$i]['timestamp'] = \common\helpers\Util::tranTime($user_achieved[intval($achievement[$i]['id'])]);
                $achievement[$i]['status'] = 1;
            }else{
                $achievement[$i]['timestamp'] = "";
            }
        }
        $result = array();
        $result["items"] = $achievement;
        return $result; 
    }
}
